import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:ok_github/page/route/route.dart';
import 'package:ok_github/repo/login_repo.dart';
import 'package:ok_github/utils/route_helper.dart';
import 'package:ok_github/widget/logo.dart';
import 'package:ok_ui/flutter_ok_ui.dart';
import 'package:oktoast/oktoast.dart';

import '../dialog/theme_color_dialog.dart';

class LoginPage extends StatefulWidget {
  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  TextEditingController nameController;
  TextEditingController pwdController;

  LoginRepo _loginRepo = LoginRepo();

  bool saveLogin = true;

  @override
  void initState() {
    super.initState();
    nameController = TextEditingController();
    pwdController = TextEditingController();

    _loginRepo.loadSavedUsername().then((user) {
      nameController.text = user;
    });
    _loginRepo.loadSavedPassword().then((password) {
      pwdController.text = password;
    });

    saveLogin = _loginRepo.saveLoginState;
  }

  @override
  void dispose() {
    nameController.dispose();
    pwdController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: OKAppBar(
        title: "登录",
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.colorize),
            onPressed: () => showColorPicker(context),
          ),
        ],
      ),
      body: Container(
        padding: const EdgeInsets.symmetric(horizontal: 16.0),
        child: Center(
          child: Column(
            children: <Widget>[
              Container(
                margin: const EdgeInsets.only(top: 80.0),
                width: 150,
                height: 150,
                child: Logo(),
              ),
              TextField(
                controller: nameController,
                decoration: InputDecoration(
                  labelText: "用户名",
                  prefixIcon: Icon(Icons.person),
                ),
              ),
              TextField(
                controller: pwdController,
                obscureText: true,
                decoration: InputDecoration(
                  labelText: "密码",
                  prefixIcon: Icon(Icons.https),
                ),
              ),
              Row(
                children: <Widget>[
                  Checkbox(
                    value: saveLogin,
                    onChanged: (v) => setState(() => this.saveLogin = v),
                  ),
                  Text("记住密码"),
                ],
              ),
              OKTextButton(
                onTap: _login,
                text: "登 录",
                radius: 10.0,
                width: double.infinity,
                margin: const EdgeInsets.symmetric(
                  vertical: 20.0,
                ),
              )
            ],
          ),
        ),
      ),
    );
  }

  void _login() async {
    var username = nameController.text;
    var password = pwdController.text;
    var result = await _loginRepo.login(username, password, saveLogin);
    if (result == true) {
      showToast("登录成功");
//      Navigator.of(context).pushNamedAndRemoveUntil(
//        routes.home,
//        ModalRoute.withName(routes.observer.rootName),
//      );

      RouteHelper.pushNamed(context, routes.home,replaceRoot: true);

    }
  }
}
